const util=require('../../utils/util')
// pages/record/record.js
Page({
  data: {
    account_id:'1',
    account_photo:'',
    account_name:'',
    // 今日是否检测的标志
    checkedFlag:0, 
    actionSheetHidden: true,   //作为开关控制弹窗是否从底部弹出
    date:'',   //当天日期
    //这个是给已经检测过的日期打上小圆点
    spotMap: {},
     // 需要改变日期时所使用的字段
     changeTime: '',
     // 存储已经获取过的日期
     dateListMap: [],
  },
    // 获取日期数据，通常用来请求后台接口获取数据
    getDateList({ detail }) {
      // 检查是否已经获取过该月的数据
      if (this.filterGetList(detail)) {
        // 获取数据
        // console.log(detail, '获取数据');
      }
    },
    // 过滤重复月份请求的方法
    filterGetList({ setYear, setMonth }) {
      const dateListMap = new Set(this.data.dateListMap);
      const key = `y${setYear}m${setMonth}`;
      if (dateListMap.has(key)) {
        return false;
      }
      dateListMap.add(key);
      this.setData({
        dateListMap: [...dateListMap],
      });
      return true;
    },
    // 日期改变的回调
    selectDay({ detail }) {
      // console.log(detail, 'selectDay detail');
    },
    // 展开收起时的回调
    openChange({ detail }) {
      // console.log(detail, 'openChange detail');
    },
    changetime() {
      this.setData({
        changeTime: '2022/1/1',
      });
    },
    //将输入的内容绑定到 msg 中
 obtainInput: function(data) {
  msg = data.detail.value;
},

listenerButton: function() {
 this.setData({
   actionSheetHidden: !this.data.actionSheetHidden
 });
},

listenerActionSheet:function() {
 this.setData({
  actionSheetHidden: !this.data.actionSheetHidden
 })
},
   /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that=this;
    var date=util.formatDate(new Date());
    const app=getApp();
    that.setData({
      account_id:app.globalData.account_id,
      date:date
    })
    
    wx.request({
      url: 'http://localhost:8080/employee/information/'+that.data.account_id,
      method:'GET',
      success(res){
        that.setData({
          account_photo:res.data.photo,
          account_name:res.data.name,
        })
      }
    })

    //检验今日是否已经检测
    wx.request({
      url: 'http://localhost:8080/report/getCheckCalendar/'+that.data.account_id,
      method:'GET',
      success(res){
        console.log(res);
        // 获取当前日期
        let currentDate = new Date();
        let currentYear = currentDate.getFullYear();
        let currentMonth = currentDate.getMonth() + 1; 
        let currentDay = currentDate.getDate();
        let currentKey = `y${currentYear}m${currentMonth}d${currentDay}`;
        let spotMap = {};
        let checkedFlag = 0;
        res.data.forEach(dateStr => {
          let date = new Date(dateStr);
          let year = date.getFullYear();
          let month = date.getMonth() + 1;
          let day = date.getDate();
          let key = `y${year}m${month}d${day}`;
          spotMap[key] = 'spot';
          // 检查是否包含当天的日期
          if (key === currentKey) {
            checkedFlag = 1;
          }
        });
        that.setData({
          spotMap: spotMap,
          checkedFlag: checkedFlag
        });
      }
    })
    console.log(that.data.spotMap);
    console.log(that.data.checkedFlag);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().init();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },


})
